import AudioTool from "./AudioTool";
import DataManager from "./DataManager";
import NetUtil from "./NetUtil";
import SDKUtil, { ApiType } from "./SDK/SDKUtil";
import Utils from "./Utils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class startPane extends cc.Component {

    @property(cc.Node)
    btn_Music: cc.Node = null;

    @property(cc.Node)
    btn_sound: cc.Node = null;

    @property(cc.Node)
    btn_share: cc.Node = null;

    @property(cc.Node)
    btn_start: cc.Node = null;

    @property(cc.Node)
    btn_rank: cc.Node = null;

    @property(cc.Node)
    off_Effect: cc.Node = null;
    @property(cc.Node)
    off_Bgm: cc.Node = null;
    private _isCreat: boolean = false;
    private _isCreat2: boolean = false;

    onLoad() {
        let times = 0;
        this.login(times);
    }

    //登陆
    login(times, cb?) {
        NetUtil.Instance.sendLogin(() => {
            if (cb) cb()
        }, () => {
            times++;
            if (times < 3) (
                this.scheduleOnce(() => {
                    this.login(times);
                }, 1)
            )
        });
    }

    start() {
        Utils.Instance.registerBtnEvent(this.btn_Music, this.onBGMBtn, this);
        Utils.Instance.registerBtnEvent(this.btn_sound, this.onSoundBtn, this);
        Utils.Instance.registerBtnEvent(this.btn_share, this.onShareBtn, this);
        //Utils.Instance.registerBtnEvent(this.btn_start, this.onStartBtn, this);
        Utils.Instance.registerBtnEvent(this.btn_rank, this.onRankBtn, this);
        this.scheduleOnce(() => {
            SDKUtil.Instance.init();
        }, 0.2)
        this.scheduleOnce(() => {
            SDKUtil.Instance.init2();
        }, 1)
        this.scheduleOnce(() => {
            if (!cc.find("Canvas/gamePanel")) SDKUtil.Instance.AdsShow(0);
        }, 0.8)
        this.setBtnState();
        Utils.Instance.poLoadBundle(() => {
            AudioTool.Instance.preLoadAudio(["btn_click", "touch", "hit", "success"]);
            AudioTool.Instance.playBGM("bgm");
        })
    }

    setBtnState() {
        cc.log("音效：", DataManager.Instance.getEffectIsOff(), "BGM：", DataManager.Instance.getBgmIsOff())
        this.off_Effect.active = DataManager.Instance.getEffectIsOff();
        this.off_Bgm.active = DataManager.Instance.getBgmIsOff();
    }

    onShareBtn() {
        SDKUtil.Instance.ShareApp(4);
    }

    onBGMBtn() {
        AudioTool.Instance.switchBGM();
        this.setBtnState();
    }

    onSoundBtn() {
        AudioTool.Instance.switchEffect();
        this.setBtnState();
    }

    onRankBtn() {
        let panel = cc.find("Canvas/ui/rankPanel");
        if (panel) {
            panel.active = true;
            panel.getComponent("rankPanel").init();
            return;
        }
        if (this._isCreat2) return;
        this._isCreat2 = true;
        Utils.Instance.creatPanel("rankPanel", (rankPanel: cc.Node) => {
            rankPanel.zIndex = 999;
            this._isCreat2 = false;
        }, { isFriendRank: true });
    }

    onStartBtn() {
        let panel = cc.find("Canvas/gamePanel");
        if (panel) {
            panel.active = true;
            panel.getComponent("GameMgr").init();
        }
        else {
            if (this._isCreat) return;
            this._isCreat = true;
            Utils.Instance.creatPanel("gamePanel", (gamePanel) => {
                if (gamePanel) {
                    gamePanel.getComponent("GameMgr").init();
                    this.node.active = false;
                }
                this._isCreat = false;
            }, "", cc.find("Canvas"));
            return;
        }
        this.node.active = false;
    }
}
